/****private function***/

function __init(){
	//$addEvent($('addBtn') , 'mousedown' , showAddWindow);
	//$addEvent($('saveBtn') , 'click' , ajaxAddUser);
	//$addEvent($('cancelBtn') , 'mousedown' ,hideAddWindow);
	
	$addEvent($('userList') , "click" , setActionWindow);
	//$addEvent($('searchBtn') , 'click' , searchAppUser);
}


function addFn(){
	var text = http_request.responseText;
	var rdata = eval("("+text+")");
	var uauth = rdata.auth==1?"Administrator":"ComUser";
	
	var newdata = [rdata.id , rdata.name , rdata.pwd , rdata.nick , uauth];
	var len = newdata.length;
	var newTr = document.createElement("tr");
	newTr.style.backgroundColor = "#DDDDDD";
	newTr.setAttribute("tag" , rdata.id);
	
	var newUp = document.createElement('input');
	newUp.type="button";
	newUp.className = "login_submit3";
	newUp.value="更新";
	newUp.setAttribute("userid" , rdata.id);
	newUp.setAttribute("tag" , "update");
	
	var newDel = document.createElement('input');
	newDel.type="button";
	newDel.className = "login_submit3";
	newDel.value="删除";
	newDel.setAttribute("userid" , rdata.id);
	newDel.setAttribute("tag" , "delete");

	var newT = document.createElement("text");
	newT.innerHTML ="&nbsp;&nbsp;";
	newTr.appendChild(newUp);
	newTr.appendChild(newT);
	newTr.appendChild(newDel);

	$addEvent(newUp , "click" , setActionWindow);
	$addEvent(newDel , "click" , setActionWindow);

	for(var i=0;i<len;i++){
		var newTd = document.createElement("td");
		newTd.innerHTML = newdata[i];
		newTr.appendChild(newTd);
	}
	$('userList').getElementsByTagName('tbody')[0].appendChild(newTr);

	hideAddWindow();
}

function modFn(el){
	var text = http_request.responseText;
	if(text.indexOf('fail')!=-1){
		alert("Update Failed!");
		var tr = el.parentNode.parentNode;
		var tds = tr.getElementsByTagName("td");
		tds[2].getElementsByTagName('input')[0].className = tds[3].getElementsByTagName('input')[0].className = "hideInput";
		tds[2].getElementsByTagName('input')[0].readOnly = tds[3].getElementsByTagName('input')[0].readOnly = true;
		tds[5].getElementsByTagName("input")[0].style.display = "none";
		tds[5].getElementsByTagName("input")[1].style.display = "";
	}
	else{
		alert("Update Succeed!");
	}
}

function delFn(){
	var text = http_request.responseText;
	var trs = $('userList').getElementsByTagName('tr');
	for(var i=1 , l=trs.length-2;i<l;i++){
		if(trs[i].getAttribute("tag")==text)	
			$('userList').getElementsByTagName('tbody')[0].removeChild(trs[i]);
	}
	alert("Delete Succeed!");
}

function setActionWindow(e){
	var el = getSrcEl(e);
	if(el.tagName=="INPUT"){
		var action=el.getAttribute("tag");
		var userid = el.getAttribute("userid");
		if(action=="add"){
			showAddWindow();
		}
		if(action=="modify"){
			makeModifyUser(el);
		}
		if(action=="saveModify"){
			ajaxModifyUser(el , userid);
		}
		if(action=="delete"){
			ajaxDeleteUser(userid);
		}
	}
}

function ajaxAddUser(){
	var action = "action=adduser";
	var username = "&username="+encodeURI($('username').value);
	var userpwd = "&userpwd="+encodeURI($('userpwd').value);
	var userauth = "&userauth="+encodeURI($('userauth').value);
	var usernick = "&usernick="+encodeURI($('usernick').value);
	var param = action+username+userpwd+userauth+usernick;
	makeAjaxRequest(ajaxUrl , param , true , addFn);
}

function makeModifyUser(el){
	var tr = el.parentNode.parentNode;
	var tds = tr.getElementsByTagName("td");
	tds[2].getElementsByTagName('input')[0].className = tds[3].getElementsByTagName('input')[0].className = "showInput";
	tds[2].getElementsByTagName('input')[0].readOnly = tds[3].getElementsByTagName('input')[0].readOnly = false;
	tds[5].getElementsByTagName("input")[0].style.display = "none";
	tds[5].getElementsByTagName("input")[1].style.display = "";
}

function ajaxModifyUser(el , uid){
	var tr = el.parentNode.parentNode;
	var tds = tr.getElementsByTagName("td");
	var language = tds[2].getElementsByTagName('input')[0].value.trim();
	var password = tds[3].getElementsByTagName('input')[0].value.trim();

	if(language==""||password==""){
		alert("please fulfill the require fields");
		return;
	}

	tds[2].getElementsByTagName('input')[0].className = tds[3].getElementsByTagName('input')[0].className = "hideInput";
	tds[2].getElementsByTagName('input')[0].readOnly = tds[3].getElementsByTagName('input')[0].readOnly = true;
	tds[5].getElementsByTagName("input")[0].style.display = "";
	tds[5].getElementsByTagName("input")[1].style.display = "none";
	
	var action = "action=moduser";
	var userid = "&userid="+encodeURI(uid);
	var language = "&language="+encodeURI(language);
	var password = "&password="+encodeURI(password);
	var param = action+userid+language+password;
	makeAjaxRequest(ajaxUrl , param , true , modFn.pass(el));
}

function ajaxDeleteUser(uid){
	if(confirm("Do you really want to delete the user?The action can't be recovered!")){
		var action = "action=deluser";
		var userid = "&userid="+encodeURI(uid);
		var param = action+userid;
		makeAjaxRequest(ajaxUrl , param , true , delFn);
	}
}

__domready();